package com.ymatou.iapi.reachpromotion.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.marketingmanage.parameter.CreateSellerPromotionBean;
import com.ymatou.iapi.reachpromotion.parameter.GetProductPromotionPolicyBean;
import com.ymatou.iapi.reachpromotion.parameter.GetSingleProductPromotionPolicyRes;
import com.ymatou.iapi.reachpromotion.parameter.PromotionPolicy;
import com.ymatou.iapi.reachpromotion.service.GetProductPromotionPolicyCall;
import com.ymatou.iapi.reachpromotion.service.PromotionInfoVerify;
import com.ymatou.iapi.trading.operate.parameter.enums.PromotionTypeEnum;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JSellerPromotionCallService;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.compare.AssertHelper;
/**
*获取买手商品促销策略信息
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetProductPromotionPolicy{
	private static GetProductPromotionPolicyBean getproductpromotionpolicyBean;
	private static GetProductPromotionPolicyCall getproductpromotionpolicyCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取买手商品促销策略信息");
	}

	@Before
	public void caseUp() {
		getproductpromotionpolicyBean=new GetProductPromotionPolicyBean();
		getproductpromotionpolicyCall=new GetProductPromotionPolicyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getproductpromotionpolicyCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductPromotionPolicy_001() {
		Logger.start(true,"获取买手商品促销策略信息");
		try {
			int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));			
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			CreateSellerPromotionBean createSellerPromotionBean = JSellerPromotionCallService.initCreateSellerPromotionBeanForSpeicalProduct(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
			int promotionId = JSellerPromotionCallService.CreateSellerPromotion(createSellerPromotionBean);
			Thread.sleep(1000);
			
			getproductpromotionpolicyBean.setSellerId(sellerid);
			getproductpromotionpolicyBean.setProductId(createSellerPromotionBean.getPromotionInfo().getProductIds().get(0));
			getproductpromotionpolicyCall.setData(getproductpromotionpolicyBean);
			getproductpromotionpolicyCall.callService();		
			
			Logger.verifyEquals("200", getproductpromotionpolicyCall.getCode(), "验证返回Code");
			GetSingleProductPromotionPolicyRes resp = getproductpromotionpolicyCall.getSingleProductPromotionPolicyRes();
			Logger.verifyEquals(getproductpromotionpolicyBean.getProductId(), resp.getProductId(), "验证返回Code");

			// 验证PromotionInfo
			List<String> productIds = createSellerPromotionBean.getPromotionInfo().getProductIds();
			for (PromotionPolicy promotionPolicy : resp.getPromotionPolicys()) {
				
				Logger.verifyEquals(promotionId, promotionPolicy.getPromotionId(), "验证返回promotionId");

				PromotionInfoVerify promotionInfoVerify = new PromotionInfoVerify();
				promotionInfoVerify.verifyPromotionPolicyInDB(promotionPolicy);
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductPromotionPolicy_002() {
		Logger.start(true,"获取买手商品促销策略信息-活动已取消");
		try {
			int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));			
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			CreateSellerPromotionBean createSellerPromotionBean = JSellerPromotionCallService.initCreateSellerPromotionBeanForSpeicalProduct(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
			int promotionId = JSellerPromotionCallService.CreateSellerPromotion(createSellerPromotionBean);
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			getproductpromotionpolicyBean.setSellerId(sellerid);
			getproductpromotionpolicyBean.setProductId(createSellerPromotionBean.getPromotionInfo().getProductIds().get(0));
			getproductpromotionpolicyCall.setData(getproductpromotionpolicyBean);
			getproductpromotionpolicyCall.callService();		
			
			Logger.verifyEquals("200", getproductpromotionpolicyCall.getCode(), "验证返回Code");
			Logger.verifyEquals("null", getproductpromotionpolicyCall.getData(), "验证返回Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductPromotionPolicy_003() {
		Logger.start(true,"获取买手商品促销策略信息-活动未开始");
		try {
			int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));			
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			CreateSellerPromotionBean createSellerPromotionBean = JSellerPromotionCallService.initCreateSellerPromotionBeanForSpeicalProduct(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
			
			createSellerPromotionBean.getPromotionInfo().setPromotionBeginTime(YMTDateUtil.getBeforeOrNextDay(1, YMTDateUtil.YMDTHMS_STRING));
			createSellerPromotionBean.getPromotionInfo().setPromotionEndTime(YMTDateUtil.getBeforeOrNextDay(2, YMTDateUtil.YMDTHMS_STRING));
			
			int promotionId = JSellerPromotionCallService.CreateSellerPromotion(createSellerPromotionBean);
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			getproductpromotionpolicyBean.setSellerId(sellerid);
			getproductpromotionpolicyBean.setProductId(createSellerPromotionBean.getPromotionInfo().getProductIds().get(0));
			getproductpromotionpolicyCall.setData(getproductpromotionpolicyBean);
			getproductpromotionpolicyCall.callService();		
			
			Logger.verifyEquals("200", getproductpromotionpolicyCall.getCode(), "验证返回Code");
			Logger.verifyEquals("null", getproductpromotionpolicyCall.getData(), "验证返回Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetProductPromotionPolicy_004() {
		Logger.start(true,"获取买手商品促销策略信息-sellerid空");
		try {
			int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));			
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			CreateSellerPromotionBean createSellerPromotionBean = JSellerPromotionCallService.initCreateSellerPromotionBeanForSpeicalProduct(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
				
			int promotionId = JSellerPromotionCallService.CreateSellerPromotion(createSellerPromotionBean);
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

//			getproductpromotionpolicyBean.setSellerId(sellerid);
			getproductpromotionpolicyBean.setProductId(createSellerPromotionBean.getPromotionInfo().getProductIds().get(0));
			getproductpromotionpolicyCall.setData(getproductpromotionpolicyBean);
			getproductpromotionpolicyCall.callService();		
			
			Logger.verifyEquals("201", getproductpromotionpolicyCall.getCode(), "验证返回Code");
			Logger.verifyEquals("请求处理失败|sellerid <= 0", getproductpromotionpolicyCall.getMsg(), "验证返回Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetProductPromotionPolicy_005() {
		Logger.start(true,"获取买手商品促销策略信息-product空");
		try {
			int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));			
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			CreateSellerPromotionBean createSellerPromotionBean = JSellerPromotionCallService.initCreateSellerPromotionBeanForSpeicalProduct(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
			
			createSellerPromotionBean.getPromotionInfo().setPromotionBeginTime(YMTDateUtil.getBeforeOrNextDay(1, YMTDateUtil.YMDTHMS_STRING));
			createSellerPromotionBean.getPromotionInfo().setPromotionEndTime(YMTDateUtil.getBeforeOrNextDay(2, YMTDateUtil.YMDTHMS_STRING));
			
			int promotionId = JSellerPromotionCallService.CreateSellerPromotion(createSellerPromotionBean);
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			getproductpromotionpolicyBean.setSellerId(sellerid);
//			getproductpromotionpolicyBean.setProductId(createSellerPromotionBean.getPromotionInfo().getProductIds().get(0));
			getproductpromotionpolicyCall.setData(getproductpromotionpolicyBean);
			getproductpromotionpolicyCall.callService();		
			
			Logger.verifyEquals("200", getproductpromotionpolicyCall.getCode(), "验证返回Code");
			Logger.verifyEquals("null", getproductpromotionpolicyCall.getData(), "验证返回Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetProductPromotionPolicy_006() {
		Logger.start(true,"获取买手商品促销策略信息-sellerid与product不一致");
		try {
			int sellerid = Integer.valueOf(EnvSetup.getData("couponSellerId"));			
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			CreateSellerPromotionBean createSellerPromotionBean = JSellerPromotionCallService.initCreateSellerPromotionBeanForSpeicalProduct(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
			
			createSellerPromotionBean.getPromotionInfo().setPromotionBeginTime(YMTDateUtil.getBeforeOrNextDay(1, YMTDateUtil.YMDTHMS_STRING));
			createSellerPromotionBean.getPromotionInfo().setPromotionEndTime(YMTDateUtil.getBeforeOrNextDay(2, YMTDateUtil.YMDTHMS_STRING));
			
			int promotionId = JSellerPromotionCallService.CreateSellerPromotion(createSellerPromotionBean);
			JSellerPromotionCallService.CancelOrEndPromotion(sellerid);

			getproductpromotionpolicyBean.setSellerId(3383);
			getproductpromotionpolicyBean.setProductId(createSellerPromotionBean.getPromotionInfo().getProductIds().get(0));
			getproductpromotionpolicyCall.setData(getproductpromotionpolicyBean);
			getproductpromotionpolicyCall.callService();		
			
			Logger.verifyEquals("200", getproductpromotionpolicyCall.getCode(), "验证返回Code");
			Logger.verifyEquals("null", getproductpromotionpolicyCall.getData(), "验证返回Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_GetProductPromotionPolicy_007() {
		Logger.start(true,"获取买手商品促销策略信息-新老接口对比");
		try {
			int sellerid = TestDataManager.getRandomIntNum(6);
		
			JSellerPromotionCallService.OldCancelOrEndPromotion(sellerid);

			CreateSellerPromotionBean createSellerPromotionBean = JSellerPromotionCallService.initCreateSellerPromotionBeanForSpeicalProduct(sellerid, PromotionTypeEnum.ReduceIfCountEnough);
			int promotionId = JSellerPromotionCallService.CreateOldSellerPromotion(createSellerPromotionBean);
			
			Thread.sleep(2000);
			
//			int sellerid = 175928;			
//			int promotionId = 7061;
//			String productId="8F026C60-1361-4CF2-94E4-EA08D83FC44D";
			
			getproductpromotionpolicyBean.setSellerId(sellerid);
			getproductpromotionpolicyBean.setProductId(createSellerPromotionBean.getPromotionInfo().getProductIds().get(0));
//			getproductpromotionpolicyBean.setProductId(productId);
			getproductpromotionpolicyCall.setData(getproductpromotionpolicyBean);
			getproductpromotionpolicyCall.callService();		
			
			Logger.verifyEquals("200", getproductpromotionpolicyCall.getCode(), "验证返回Code");
			GetSingleProductPromotionPolicyRes resp = getproductpromotionpolicyCall.getSingleProductPromotionPolicyRes();
			Logger.verifyEquals(getproductpromotionpolicyBean.getProductId(), resp.getProductId(), "验证返回Code");

			// 验证PromotionInfo
			List<String> productIds = createSellerPromotionBean.getPromotionInfo().getProductIds();
			for (PromotionPolicy promotionPolicy : resp.getPromotionPolicys()) {
				
				Logger.verifyEquals(promotionId, promotionPolicy.getPromotionId(), "验证返回promotionId");

				PromotionInfoVerify promotionInfoVerify = new PromotionInfoVerify();
				promotionInfoVerify.verifyPromotionPolicyInDB(promotionPolicy);
			}
			
			String url = getproductpromotionpolicyCall.getUrl();
			url=url.replace("reachpromotion.iapi.ymatou.com", "sellerpromotion.api.ymatou.com");
		
			getproductpromotionpolicyCall.setUrl(url);
			
			getproductpromotionpolicyCall.callService();		
			
			Logger.verifyEquals("200", getproductpromotionpolicyCall.getCode(), "验证返回Code");
			GetSingleProductPromotionPolicyRes resp2 = getproductpromotionpolicyCall.getSingleProductPromotionPolicyRes();
			Logger.verifyEquals(getproductpromotionpolicyBean.getProductId(), resp2.getProductId(), "验证返回Code");

			// 验证PromotionInfo
			List<String> productIds2 = createSellerPromotionBean.getPromotionInfo().getProductIds();
			for (PromotionPolicy promotionPolicy : resp2.getPromotionPolicys()) {
				
				Logger.verifyEquals(promotionId, promotionPolicy.getPromotionId(), "验证返回promotionId");

				PromotionInfoVerify promotionInfoVerify = new PromotionInfoVerify();
				promotionInfoVerify.verifyPromotionPolicyInDB(promotionPolicy);
			}
			
			AssertHelper.assertResultEqual(resp, resp2, "");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	//{ "em" : 0,"bt":{"$lt":ISODate("2017-06-15T03:01:52.560Z")},"et":{"$gt":ISODate("2017-06-15T03:01:52.560Z")}}
	public void Tc_GetProductPromotionPolicy_008() {
		Logger.start(true,"获取买手商品促销策略信息-新老接口对比-stg");
		try {
			int sellerid = 3207973;			
			int promotionId = 16983;
			String productId="4a1c7bb7-195c-469c-a44f-8e3b45e8f3e8";
			
			getproductpromotionpolicyBean.setSellerId(sellerid);
			getproductpromotionpolicyBean.setProductId(productId);
			getproductpromotionpolicyCall.setData(getproductpromotionpolicyBean);
			getproductpromotionpolicyCall.callService();		
			
			Logger.verifyEquals("200", getproductpromotionpolicyCall.getCode(), "验证返回Code");
			GetSingleProductPromotionPolicyRes resp = getproductpromotionpolicyCall.getSingleProductPromotionPolicyRes();
			Logger.verifyEquals(getproductpromotionpolicyBean.getProductId(), resp.getProductId(), "验证返回Code");
			
			String url = getproductpromotionpolicyCall.getUrl();
			url=url.replace("reachpromotion.iapi.ymatou.com", "sellerpromotion.api.ymatou.com");
		
			getproductpromotionpolicyCall.setUrl(url);
			
			getproductpromotionpolicyCall.callService();		
			
			Logger.verifyEquals("200", getproductpromotionpolicyCall.getCode(), "验证返回Code");
			GetSingleProductPromotionPolicyRes resp2 = getproductpromotionpolicyCall.getSingleProductPromotionPolicyRes();
			Logger.verifyEquals(getproductpromotionpolicyBean.getProductId(), resp2.getProductId(), "验证返回Code");
	
			AssertHelper.assertResultEqual(resp, resp2, "GetProductPromotionPolicy");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}